/*============================================================================*/

* Do: Regressions Part 2 - ENAHO
* Last update: 09.05.2023

/*============================================================================*/

* SECTION: FOLDERS
* ==============================================================================

*------------------------ SUBFOLDERS (DO NOT CHANGE)
gl in "$root/Final"
gl analysis "$root/Analysis"

* ==============================================================================
* SECTION: REGRESSIONS
* ==============================================================================
cap log close
log using "regressions_part2_lapop", replace

use "$in/data_regressions_lapop", clear

xtset, clear

gen highemp = formalemployrate07>=.13

* Outcomes

local outcome victimcrime neighorhood_safety quality_localservices trust_neighbors employment_status 

* Covariates

local indvchar c.age##c.age ib1.education_group i.marital_status hhsize  

local work employment_status

local iv c.logsharevz07#(i2017.year i2019.year)


*------------------------ DESCRIPTIVES 

su `outcome' `indvchar' `work' logsharevz totnumvz_prov totptp_prov, sep(0)


*------------------------ REGRESSIONS

forv i = 0/2 {

if `i'~=2 {
local sex "if female==`i'"
}

else {
local sex "female"
}

* first-stage

reghdfe logsharevz `iv' `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.year c.time#i.province_id)
testparm `iv', equal


foreach y of local outcome {

		if "`y'" == "victimcrime" { 
		local ra replace 
		}
	
		else { 
		local ra append 
		}


	 if  ~inlist("`y'", "employment_status") { 

		if `i'==2 {

		reghdfe `y' logsharevz `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.year) 
			outreg2 using "$analysis/part2la_1`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, No)

		reghdfe `y' logsharevz `indvchar' `work'  `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.year)
			outreg2 using "$analysis/part2la_2`i'.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, Yes) 
		}

		ivreghdfe `y' (logsharevz=`iv') `indvchar' `work'  `sex', cluster(province_id) absorb(i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2la_3`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, Yes) 
		}

        if  inlist("`y'", "employment_status") { 

		if `i'==2 {

		reghdfe `y' logsharevz `indvchar' `sex', cluster(province_id) absorb(province_id i.year) 
			outreg2 using "$analysis/part2la_1`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, No)

		reghdfe `y' logsharevz `indvchar' `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.year)
			outreg2 using "$analysis/part2la_2`i'.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, Yes) 
		}

		ivreghdfe `y' (logsharevz=`iv') `indvchar' `sex', cluster(province_id) absorb(i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2la_3`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, RegionXTime trends, Yes) 

		}

}
}


log close

